﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;


namespace Main
{
    class GeneralTools
    {
        /// <summary>
        /// 读配置文件
        /// </summary>
        #region
        public string getConfigByName(string module, string confname)
        {
            string strVEImageDir = System.Windows.Forms.Application.StartupPath + "\\config";

            string confVal = "";

            if (!Directory.Exists(strVEImageDir))
            {
                Directory.CreateDirectory(strVEImageDir);
            }
            try
            {
                XDocument document = XDocument.Load(strVEImageDir + "\\GATHERCONF.xml");
                XElement root = document.Root;
                foreach (XElement element in document.Root.Elements(module))
                {
                    foreach (XElement element1 in element.Elements(confname))
                    {
                        confVal = element1.Value;
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
            return confVal;
        }
        public  Dictionary<String,String> getConfigs() {
            Dictionary<String, String> paramMap = new Dictionary<string, string>();
            string strConfigDir = System.Windows.Forms.Application.StartupPath + "\\config";

            string confVal = "";

            if (!Directory.Exists(strConfigDir))
            {
                Directory.CreateDirectory(strConfigDir);
            }
            try
            {
                XDocument document = XDocument.Load(strConfigDir + "\\GATHERCONF.xml");
                XElement root = document.Root;
                foreach (XElement element in document.Root.Elements("GENERAL"))
                {
                    foreach (XElement subElement in element.Elements())
                    {
                        string key = subElement.Name.ToString();
                        string value = subElement.Value;
                        paramMap.Add(key, value);
                    }
                }

                
            }
            catch (Exception ex)
            {
                //WriteLog.printException(ex);
            }
            return paramMap;
        }

        public static Dictionary<String, String> getConfigs(string _module)
        {
            Dictionary<String, String> paramMap = new Dictionary<string, string>();
            string strConfigDir = System.Windows.Forms.Application.StartupPath + "\\config";

            string confVal = "";

            if (!Directory.Exists(strConfigDir))
            {
                Directory.CreateDirectory(strConfigDir);
            }
            try
            {
                XDocument document = XDocument.Load(strConfigDir + "\\GATHERCONF.xml");
                XElement root = document.Root;
                foreach (XElement element in document.Root.Elements(_module))
                {
                    foreach (XElement subElement in element.Elements())
                    {
                        string key = subElement.Name.ToString();
                        string value = subElement.Value;
                        paramMap.Add(key, value);
                    }
                }


            }
            catch (Exception ex)
            {
                //WriteLog.printException(ex);
            }
            return paramMap;
        }
        #endregion

        /**
         * 设备及系统日志
         * logType ： LED HK RD IC
         *
         * **/
        #region
        public static void SaveLog(String logType, string logContent)
        {
            FileStream fs = null;
            try
            {
                string fileFolder = AppDomain.CurrentDomain.BaseDirectory.ToString() + "log" + "\\" + logType;
                if (!Directory.Exists(fileFolder))
                {
                    Directory.CreateDirectory(fileFolder);
                }
                string filePath = fileFolder + "\\" + setLogFile();

                if (!File.Exists(filePath))
                {
                    fs = File.Create(filePath);
                }
                else
                {
                    fs = File.Open(filePath, FileMode.Append);
                }
                string strToWrite = "\r\n" + "【 " + System.DateTime.Now.ToString() + "】" + "   " + logContent
                                  + "\r\n" + "【 " + System.DateTime.Now.ToString() + "】" + "   " + "============================================================";
                byte[] b = System.Text.Encoding.Default.GetBytes(strToWrite);
                fs.Write(b, 0, b.Length);
                fs.Close();
            }
            catch
            { }
            finally
            {
                if (fs != null)
                    fs.Close();
            }
        }

        //获取日志的名称，按天
        public static string setLogFile()
        {
            System.Text.StringBuilder sb = new System.Text.StringBuilder();
            string date = System.DateTime.Today.ToString("yyyy-MM-dd");
            sb.Append("KF--" + date);
            sb.Append(".log");

            return sb.ToString();
        }
        #endregion
    }
}